<template>
	<van-cell-group class="addressList">
		<van-nav-bar title="地址编辑" left-arrow @click-left="back"/>
        <van-address-edit
          :address-info="form"
          :area-list="areaList"
          show-postal
          show-delete
          show-set-default
          show-search-result
          @save="onSave"
          @delete="onDelete"
        />
	</van-cell-group>
</template>
<script>
import { CellGroup,NavBar,AddressEdit,Dialog} from 'vant';

export default {
	props: {
        callback:{
            type: Function,
        },
        popup :{
        	type : Boolean,
        	default : false
        }
    },
    components: {
    	[CellGroup.name] : CellGroup,
    	[NavBar.name] : NavBar,
        [AddressEdit.name] : AddressEdit,
        [Dialog.name] : Dialog,
    },
    data () {
        return {
            areaList :{} ,
            form :{},
        }
    },
    methods :{
        init(id){
            if (id) {
                this.getAddress(id);
            }else{
                this.form = {};
            }
            this.getAreaList();
        },
        getAddress(id){
            this.Axios.Home.Address.Get(id)
            .then(res =>{
                if(!res) return false;
                this.form = res.data;
            })
        },
        getAreaList(){
            this.Axios.Home.Address.GetAllDistrict()
            .then(res =>{
                if(!res) return false;
                this.areaList = res.data;
            })
        },
        onSave(form) {
            this.Axios.Home.Address.Update(form)
            .then(res =>{
                if(!res) return false;
                this.back();
            })
        },
        onDelete(form) {
            this.Axios.Home.Address.Delete({id:form.id})
            .then(res =>{
                if(!res) return false;
                this.back();
            })
        },
        back(){
        	if (this.popup) {
        		this.callback();
        	}else{
        		window.history.go(-1);
        	}
        },
    },
}
</script>
<style>
	.addressList .van-address-list {padding-bottom:0px;}
</style>